package com.iyao_app.fragment;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.flipboard.bottomsheet.commons.BottomSheetFragment;
import com.iyao_app.R;
import com.iyao_app.adapter.MyViewPagerAdapter;
import com.iyao_app.adapter.NotifyFriendAdapter;
import com.iyao_app.entry.DemoData;
import com.iyao_app.entry.MyInterestBean;
import com.iyao_app.mvp.sign.SignActivity;
import com.iyao_app.util.ImageData;
import com.iyao_app.util.SpaceItemDecoration;
import com.iyao_app.view.LetterSpacingTextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/23.
 */

public class SignPersonFragment extends BottomSheetFragment {

    @BindView(R.id.tv_complete)
    LetterSpacingTextView mTvComplete;
    @BindView(R.id.viewpager)
    ViewPager mViewpager;
    @BindView(R.id.rg_indicate)
    RadioGroup mRgIndicate;

    Unbinder unbinder;

    private final int ITEM_COUNT_OF_PAGE = 9;


    private NotifyFriendAdapter notifyFriendAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sign, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        initPagerView();
    }

    private void initPagerView() {
        List<View> views = new ArrayList<View>();
        int count = ((SignActivity) getActivity()).dataList.size() / ITEM_COUNT_OF_PAGE;
        if (((SignActivity) getActivity()).dataList.size() % ITEM_COUNT_OF_PAGE != 0) {
            count++;
        }
        List<MyInterestBean> pageItem;
        for (int i = 0; i < count; i++) {
            RecyclerView recyclerView = (RecyclerView) LayoutInflater.from(getActivity())
                    .inflate(R.layout.include_recycler_view, null);
            if (i == count - 1) {
                pageItem = ((SignActivity) getActivity()).dataList.subList(i * ITEM_COUNT_OF_PAGE, ((SignActivity) getActivity()).dataList.size());
            } else {
                pageItem = ((SignActivity) getActivity()).dataList.subList(i * ITEM_COUNT_OF_PAGE, (i + 1) * ITEM_COUNT_OF_PAGE);

            }
            StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
            notifyFriendAdapter = new NotifyFriendAdapter((SignActivity) getActivity(), pageItem);
            recyclerView.addItemDecoration(new SpaceItemDecoration((int) getActivity().getResources().getDimension(R.dimen.x20)));
            recyclerView.setAdapter(notifyFriendAdapter);
            recyclerView.setLayoutManager(layoutManager);
            views.add(recyclerView);
            // 添加指示器
            addNavigation(i);//每一页添加一个RadioButton，默认选中第一个
        }
        MyViewPagerAdapter pageAdapter = new MyViewPagerAdapter(views);
        mViewpager.setAdapter(pageAdapter);
        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                RadioButton radioButton = (RadioButton) mRgIndicate.getChildAt(position);
                radioButton.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private void addNavigation(int index) {
        RadioButton rb = new RadioButton(getActivity());
        rb.setButtonDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
        rb.setBackgroundResource(R.drawable.select_notify_friend_dot);
        RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(16, 16);
        params.setMargins(10, 10, 10, 10);
        params.gravity = Gravity.CENTER;
        mRgIndicate.addView(rb, params);
        if (index == 0) {
            rb.setChecked(true);
        }
        // 点击效果
    }

    public void initData() {

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.tv_complete)
    public void onViewClicked() {
        dismiss();
    }

}
